package com.xhwl.centiotdata.pollingtrigger.operation;

import com.xhwl.centiotdata.handler.operation.OperationIntegrationFactory;
import com.xhwl.centiotdata.handler.operation.OperationIntegrationHandler;
import com.xhwl.centiotdata.pollingtrigger.ScheduleDispatcher;
import com.xhwl.centiotdata.service.integrateconfig.IntegrationConfig;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/03/ 09:25
 */
@Service
public class OperationDispatcher implements ScheduleDispatcher {

    interface OnGetConfig {
        void execute(DeviceProjSupplierCategory deviceProjSupplierCategory);
    }

    @Override
    public void doTenMinutesTask() {
        //获取经营数据
        ThreadPoolUtils.baiLianSyncExecutor.execute(() ->
                executePull(deviceProjSupplierCategory -> {
                    OperationIntegrationHandler handler = OperationIntegrationFactory.getInvokeStrategy(deviceProjSupplierCategory.getProductId());
                    handler.pullSales(deviceProjSupplierCategory);
                }));
    }

    @Override
    public void doOneDayTask() {
        //拉取店铺信息不需要太频繁
        ThreadPoolUtils.baiLianSyncExecutor.execute(() ->
                executePull(deviceProjSupplierCategory -> {
                    OperationIntegrationHandler handler = OperationIntegrationFactory.getInvokeStrategy(deviceProjSupplierCategory.getProductId());
                    handler.pullCompany(deviceProjSupplierCategory);
                }));
    }

    public void executePull(OnGetConfig onGetConfig) {
        // todo 由于无categoryId，采取如下方式
        IntegrationConfig.integrationConfigList.stream()
                .filter(integrationConfig -> integrationConfig.getProductId().intValue() == 9 && integrationConfig.getProdCategoryId() == null)
                .forEach(integrationConfig -> {
                    try {
                        onGetConfig.execute(integrationConfig);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
    }

}
